package com.x.crm.dao;

import com.x.crm.model.CustomerLevel;
import com.x.framework.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class CustomerLevelDao extends BaseDao {
	private final static String baseQuerySql = "SELECT * FROM CRM_CUSTOMER_LEVEL T1 WHERE 1=1 ";

	public List<CustomerLevel> selectCustomerLevelList(CustomerLevel customerLevel) throws Exception {
		StringBuilder sql = new StringBuilder("SELECT * FROM (SELECT * FROM CRM_CUSTOMER_LEVEL WHERE LEVEL_ID < 100 ");
		sql.append("UNION ALL ");
		sql.append(baseQuerySql);
		List<Object> params = new ArrayList<>();
		searchLevelCondition(customerLevel, sql, params);
		sql.append(")T ORDER BY T.LEVEL_CREATE_TIME DESC ");
		return this.queryForList(sql.toString(), CustomerLevel.class, params, customerLevel);
	}

	/**
	 * 查询不包含系统默认数据的参数
	 * @param customerLevel
	 * @return
	 * @throws Exception
	 */
	public List<CustomerLevel> selectLevelList(CustomerLevel customerLevel) throws Exception {
		StringBuilder sql = new StringBuilder(baseQuerySql);
		List<Object> params = new ArrayList<>();
		searchLevelCondition(customerLevel, sql, params);
		sql.append("ORDER BY T1.LEVEL_CREATE_TIME DESC ");
		return this.queryForList(sql.toString(), CustomerLevel.class, params, customerLevel);
	}


	/**
	 * 查询不包括系统配置的条数
	 *
	 * @param CustomnerLevel
	 * @return
	 * @throws Exception
	 */
	private final static String baseQueryListNumSql = "SELECT COUNT(T1.LEVEL_ID) FROM CRM_CUSTOMER_LEVEL T1 WHERE  1=1 ";

	public int selectCustomerLevelListNum(CustomerLevel customerLevel) throws Exception {
		StringBuilder sql = new StringBuilder(baseQueryListNumSql);
		List<Object> params = new ArrayList<Object>();
		searchLevelCondition(customerLevel, sql, params);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 查询条数
	 *
	 * @param CustomnerLevel
	 * @return
	 * @throws Exception
	 */

	public int selectCustomerLevelNum(CustomerLevel customerLevel) throws Exception {
		StringBuilder sql = new StringBuilder("SELECT COUNT(LEVEL_ID) FROM (SELECT * FROM CRM_CUSTOMER_LEVEL WHERE LEVEL_ID < 100 ");
		sql.append("UNION ALL ");
		sql.append(baseQuerySql);
		List<Object> params = new ArrayList<Object>();
		searchLevelCondition(customerLevel, sql, params);
		sql.append(")T ");
		return this.queryForInt(sql.toString(), params);
	}


	public void searchLevelCondition(CustomerLevel customerLevel, StringBuilder sb, List<Object> params) {
//		if (this.isNotNull(customerLevel.getMarketId())) {
//			sb.append("AND T1.MARKET_ID = ? ");
//			params.add(customerLevel.getMarketId());
//		}
		if (this.isNotNull(customerLevel.getLevelState())) {
			sb.append("AND T1.LEVEL_STATE = ? ");
			params.add(customerLevel.getLevelState());
		}
		if (this.isNotNull(customerLevel.getLevelTxt())) {
			sb.append("AND T1.LEVEL_TXT = ? ");
			params.add(customerLevel.getLevelTxt());
		}
		if (this.isNotNull(customerLevel.getLevelId())) {
			sb.append("AND T1.LEVEL_ID  = ? ");
			params.add(customerLevel.getLevelId());
		}
//		if (this.isNotNull(customerLevel.getShopId())) {
//			sb.append("AND T1.SHOP_ID = ? ");
//			params.add(customerLevel.getShopId());
//		}
	}
}
